//package com.paypalpay.timingtask;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONArray;
//import com.paypalpay.entity.appinterface.resultsynchronization.request.RequestChannelResultSynchronizationData;
//import com.paypalpay.entity.appinterface.resultsynchronization.request.RequestChannelResultSynchronizationTradeData;
//import com.paypalpay.entity.appinterface.resultsynchronization.request.RequesteChannelResultSynchronization;
//import com.paypalpay.entity.appinterface.resultsynchronization.response.ResponseChannelResultSynchronization;
//import com.paypalpay.entity.common.DevicePartnerAccess;
//import com.paypalpay.entity.common.UserboundPaymentOrder;
//import com.paypalpay.service.*;
//import com.paypalpay.util.HttpUtil;
//import com.paypalpay.util.wx.MD5;
//import com.paypalpay.util.wx.Md5Encrypt;
//import org.apache.commons.lang.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//
//import javax.servlet.http.HttpSession;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//
//@Configuration
//@EnableScheduling
////@Api(description = "同步支付数据接口")
//public class ChannelResultSynchronizationTask {
//    @Autowired
//    private SequenceService sequenceService;
//
//    @Autowired
//    private UserboundPaymentOrderService userboundPaymentOrderService;
//
//    @Autowired
//    private DevicePartnerAccessService devicePartnerAccessService;
//
//    //每30秒执行一次
//    //@Scheduled(cron = "0/30 0/1 * * * ?")
//    //@ApiOperation(value = "定时任务",response = ResponseChannelResultSynchronization.class)
//    //@ApiParam @RequestBody RequesteChannelResultSynchronization requesteChannelResultSynchronization
//    //@Scheduled(cron = "0 0 0/1 * * ?")
//    //@Scheduled(cron = "0 0/30 * * * ?")
//   //@Scheduled(cron = "0/30 * * * * ?")
//
//    //任务已经转到payment2.0上去了
//
//    public void synchronizationTask() {
//        try {
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String curtime = df.format(new Date());
//            // 1，生成流水号
//            String connSeqNo = sequenceService.getPackageTaskNo();
//            //token
//            //String token = "BAD59533A61B6F0DE07A6A68D99CBACC";
//
//            System.out.println("ChannelResultSynchronizationTask渠道结果同步定时任务正在执行！");
//            //1,去数据库查询一下有哪些记录是需要批量回传支付结果通知的
//            List<UserboundPaymentOrder> userList = userboundPaymentOrderService.getChannelResultList();
//            if (userList == null || userList.isEmpty()) {
//                System.out.println("没有查询到需要去做结果通知的数据！");
//            } else {
//                for (UserboundPaymentOrder userOrder : userList) {
//
//                    String partnerCode = userOrder.getPartnerCode();
//                    String packageName = userOrder.getPackageName();
//                    String orderId = userOrder.getOrderId();
//                    String deviceId = userOrder.getDeviceId();
//                    String deviceType = userOrder.getDeviceType();
//                    String payType = userOrder.getPayType();
//                    String payAmount = userOrder.getAmount();
//                    String payRst = userOrder.getPayRst();
//                    String requestOrderId = userOrder.getRequestOrderId();
//
//                    //2,去渠道访问的数据库把url拿出来去同步数据给渠道后台
//                    List<DevicePartnerAccess> partnerList = devicePartnerAccessService.getPartnerAccessList(partnerCode, deviceId, deviceType, requestOrderId);
//                    if (partnerList == null || partnerList.isEmpty()) {
//                        System.out.println("没有查询到需要去同步结果的数据！");
//                    } else {
//                        for (DevicePartnerAccess partnerAccess : partnerList) {
//                            String url = partnerAccess.getNotyfy_URL();
//                            //String url ="http://dytestmall100.ecomoter.com/Pay/lingNotyfy";
//                            //String url ="http://dyappfortest100.ecomoter.com/pay/lingNotyfy";
//                            String token = partnerAccess.getToken();
//                            //现在数据都准备齐了，可以开始同步了
//                            RequestChannelResultSynchronizationTradeData tradeData = new RequestChannelResultSynchronizationTradeData();
//                            tradeData.setDeviceId(deviceId);
//                            tradeData.setDeviceType(deviceType);
//                            tradeData.setRequestOrderId(requestOrderId);
//                            tradeData.setPackageName(packageName);
//                            //tradeData.setPackageName(java.net.URLEncoder.encode(packageName, "UTF-8"));
//                            tradeData.setOrderId(orderId);
//                            tradeData.setPayType(payType);
//                            tradeData.setPayAmount(payAmount);
//                            tradeData.setPayRst(payRst);
//
//                            RequestChannelResultSynchronizationData data = new RequestChannelResultSynchronizationData();
//                            data.setConnSeqNo(connSeqNo);
//                            data.setPartnerCode(partnerCode);
//                            data.setToken(token);
//                            data.setTradeTime(curtime);
//                            data.setTradeData(tradeData);
//
//                            RequesteChannelResultSynchronization result = new RequesteChannelResultSynchronization();
//                            result.setData(data);
//                            //result.setSign(MD5.calcMD5(JSON.toJSONString(data)));
//                            result.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));
//
//                            // 返回给调用者的报文
//                            String jsonString = JSON.toJSONString(result);
//                            System.out.println("地址：：" + url + ";回调内容：" + jsonString);
//
//                            // 发送post请求，并接收返回结果
//                            String returnStr = HttpUtil.httpPost(url, jsonString);
//                            System.out.println("返回的回复结果：" + returnStr);
//
//                            String str=returnStr.substring(0,1);
//
//                            if (str.equals("{")) {
//
//                                ResponseChannelResultSynchronization response = JSONArray.parseObject(returnStr, ResponseChannelResultSynchronization.class);
//
//                                if (response != null) {
//
//                                    System.out.println("得到报文回复" + ResponseChannelResultSynchronization.class);
//
//                                    int tradeRstCode = response.getData().getTradeRstCode();
//                                    String tradeRstMessage = response.getData().getTradeRstMessage();
//
//                                    if (tradeRstCode != 1000 && !"success".equalsIgnoreCase(tradeRstMessage)) {
//                                        //如果返回的结果不是1000，就不是成功！
//                                        System.out.println("同步结果返回不是1000");
//                                        return;
//                                    } else {
//                                        //说明数据推送成功了
//                                        System.out.println("同步支付结果成功");
//                                        // 所有的通知结束之后，将数据库里面的isChannelNotity变成0，即为不需要再次通知
//                                        UserboundPaymentOrder paymentOrder = new UserboundPaymentOrder();
//                                        paymentOrder.setIsChannelNotity("0");
//                                        paymentOrder.setOrderId(orderId);
//                                        paymentOrder.setRequestOrderId(requestOrderId);
//                                        userboundPaymentOrderService.updateIsChannelNotity(paymentOrder);
//                                        // 当程序跑到这一步就真的完成了
//                                        System.out.println("ChannelResultSynchronizationTask渠道结果同步定时任务执行完毕！");
//
//                                    }
//                                } else {
//                                    System.out.println("未得到成功的报文回复");
//                                    return;
//                                }
//
//                            } else {
//
//                                System.out.println("Http请求出现异常！");
//                                return;
//
//                            }
//
//                        }
//
//                    }
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}
